import { message } from 'antd';

import api from '../api';

export default {
  namespace: 'globalM',

  state: {
    collapsed: false, // 侧边菜单栏是否合并
    listNeedRefresh: false, // 更新刷新标识//
    dataDictionary: {}, // 字典
    headerHeight: 0, // 头部占用高度
  },

  effects: {
    // (t-1)获取前一交易日 type: 1:交易所 ,2:银行间
    *getTradingDate({ payload, callback }, { call }) {
      const res = yield call(api.getTradingDate, payload);
      if (res && res.message == 'success') {
        callback(res.data);
      } else {
        message.error(res?.message);
      }
    },
    // 获取数字字典
    *fetchDataDictionary(_, { call, put }) {
      const data = yield call(api.findAllDict, {});
      if (data) {
        yield put({
          type: 'save',
          payload: {
            dataDictionary: data,
          },
        });
      }
      return data;
    },

    // 获取假期
    *queryHoliday({ payload, callback }, { call }) {
      try {
        const response = yield call(api.getHolidayByMonth, payload);
        if (response) {
          if (response.code == 0) {
            callback('success', response.data);
          } else {
            callback('fail', []);
            message.error(response.msg || '获取日历汇总信息失败!');
          }
        } else {
          callback('fail');
          message.error('获取日历汇总信息失败!');
        }
      } catch (e) {
        console.log(e);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return { ...state, ...payload };
    },
    // 左侧菜单收缩切换
    changeLayoutCollapsed(state, { payload }) {
      return {
        ...state,
        collapsed: payload,
      };
    },
  },
};
